import { createRouter, createWebHistory } from 'vue-router'

const router = createRouter({
  history: createWebHistory(import.meta.env.BASE_URL),
  routes: [
    {
      path: '/',
      redirect: '/login'
    },
    {
      path: '/teacher',
      name: 'teacher',
      component: () => import('../views/index.vue'),
    },
    {
      path: '/student',
      name: 'student',
      component: () => import('../views/index.vue'),
    },
    {
      path: '/login',
      name: 'login',
      component: () => import('../views/login.vue'),
    },
    {
      path: '/register',
      name: 'register',
      component: () => import('../views/register.vue'),
    },
    {
      path: '/editor',
      name: 'components',
      component: () => import('../components/AiEditor.vue'),
    },
  ],
})

export default router

// 全局路由守卫// router/index.ts
router.beforeEach((to, from, next) => {
  const publicPages = ['/login', '/register']; // 白名单页面
  const authRequired = !publicPages.includes(to.path); // 判断是否需要登录
  const loggedIn = localStorage.getItem('satoken');

  if (authRequired && !loggedIn) {
    next('/login'); // 需要登录且未登录 → 跳转登录
  } else {
    next(); // 否则放行
  }
});

